﻿using Autofac;
using Autofac.Extras.DynamicProxy;
using Blog.Core.IRepositories;
using Blog.Core.IRepositories.JWT;
using Blog.Core.IServices.IUser;
using Blog.Core.Repositories;
using Castle.DynamicProxy;   //应用dynamicproxy
using Microsoft.Extensions.Logging; //日志ILogger<>
using Microsoft.IdentityModel.Logging;
using Newtonsoft.Json;
using System.Diagnostics; //StopWatch所在命名空间

namespace Blog.Core.Api.AutoFacdynamicFilter
{
    /// <summary>
    /// 分模块配置
    /// </summary>
    public class AutoFacdynamicFilters : Autofac.Module
    {
        ////依赖注入
        //private readonly ILogger<AutoFacdynamicFilters> _logger;
        ////构造函数
        //public AutoFacdynamicFilters(ILogger<AutoFacdynamicFilters> logger)
        //{
        //    _logger = logger;
        //}
        ///// <summary>
        ///// 拦截器接口方法
        ///// </summary>
        ///// <param name="invocation"></param>
        ///// <exception cref="NotImplementedException"></exception>
        //public void Intercept(IInvocation invocation)
        //{
        //   //执行方法前 做点什么
        //   Stopwatch stopwatch = Stopwatch.StartNew();
        //    //执行秒表
        //   stopwatch.Start();
        //    //执行方法
        //    invocation.Proceed();
        //    //执行方法后做点什么
        //    stopwatch.Stop();
        //    //输出日志
        //    string actionname = invocation.Method.Name;
        //    string classname = invocation.TargetType.Name;
        //    _logger.LogInformation($"执行了{classname}的{actionname}方法，用时{stopwatch.ElapsedMilliseconds}a毫秒\n,执行结果是{invocation.ReturnValue}");
        //}
        ///// <summary>
        ///// 拦截器
        ///// </summary>
        ///// <param name="invocation"></param>
        //public void Intercept(IInvocation invocation)
        //{

        //    string c = string.Format("Calling method {0} with parameters {1}... ",

        //    invocation.Method.Name,

        //    string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray()));
        //    //执行方法
        //    invocation.Proceed();

        //    string b = string.Format("Done: result was {0}.", invocation.ReturnValue);
        //}
        //private readonly ILogger<AutoFacdynamicFilters> _logger;
        ///// <summary>
        ///// 构造函数
        ///// </summary>
        ///// <param name="logger"></param>
        //public AutoFacdynamicFilters(ILogger<AutoFacdynamicFilters> logger)
        //{
        //    _logger = logger;
        //}
        ///// <summary>
        ///// 拦截器
        ///// </summary>
        ///// <param name="builder"></param>
        //protected override  void Load(ContainerBuilder builder)
        //{
        //    //注册拦截器到Autofac容器中
        //    builder.RegisterType<AutoFacFitter>();
        //    //AutoFac批量注册UserService所在程序集中所有Service类,并启用拦截器
        //    builder.RegisterAssemblyTypes(typeof(IUserService).Assembly)
        //        .Where(t => t.Name.EndsWith("Service"))
        //        .AsImplementedInterfaces()
        //        .PropertiesAutowired()
        //        .EnableInterfaceInterceptors()         //这里动态创建一个接口代理，另外还有一个EnableClassInterceptors方法
        //        .InterceptedBy(typeof(AutoFacFitter)); //动态注入拦截器ServiceLoggerInterceptor

        //    //注入所有的仓储
        //    builder.RegisterAssemblyTypes(System.Reflection.Assembly.Load("Blog.Core.Repositories"))
        //    .Where(x => x.Name.EndsWith("Repository"))
        //    .AsImplementedInterfaces()
        //    .InstancePerLifetimeScope();
        //    //注入工作单元
        //    builder.RegisterType<UnOfWork>().As<IUnOfWork>();
        //    builder.RegisterType<TokenHelper>().As<ITokenHelper>();

        //}
 

    }    
 }
